package com.ysyc.driver_ee.manager;

import android.app.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class ActivityManagerHelp {

    private Map<String, Integer> mActivityCount;

    private List<Activity> mActivities;

    private static ActivityManagerHelp sActivityManager;

    private ActivityManagerHelp() {
        mActivities = new ArrayList<>();
        mActivityCount = new HashMap<String, Integer>();
    }

    public static ActivityManagerHelp getInstance() {
        if (sActivityManager == null) {
            sActivityManager = new ActivityManagerHelp();
        }
        return sActivityManager;
    }


    public void add(Activity current) {
        if (current != null) {
            activityCountIncrease(current);
            this.mActivities.add(current);
        }
    }


    public void remove(Class current) {
        activityCountDecrease(current);
        Iterator moduleName1 = this.mActivities.iterator();
        while (moduleName1.hasNext()) {
            Activity temp1 = (Activity) moduleName1.next();
            if (temp1.getClass().getName().equals(current.getName())) {
                mActivities.remove(temp1);
                temp1.finish();
                break;
            }
        }
    }

    public List<Activity> getActivitiesByClass(Class current) {
        Iterator moduleName = this.mActivities.iterator();
        List<Activity> activities = new ArrayList<>();
        while (moduleName.hasNext()) {
            Activity temp = (Activity) moduleName.next();
            if (temp.getClass().getName().equals(current.getName())) {
                activities.add(temp);
            }
        }
        return activities;
    }


    public void remove(Activity activity) {
        activityCountDecrease(activity);
        Iterator moduleName1 = this.mActivities.iterator();
        while (moduleName1.hasNext()) {
            Activity temp1 = (Activity) moduleName1.next();
            if (temp1 == activity) {
                mActivities.remove(activity);
                activity.finish();
                break;
            }
        }
    }

    public void finish() {
        Iterator moduleName1 = this.mActivities.iterator();
        while (moduleName1.hasNext()) {
            Activity temp1 = (Activity) moduleName1.next();
            if (temp1 != null) {
                temp1.finish();
            }
        }

        if (mActivityCount != null) mActivityCount.clear();

    }

    public Activity getStackTopActivity() {
        if (mActivities.size() > 0) {
            return mActivities.get(mActivities.size() - 1);
        }
        return null;
    }

    public int getAliveActivity() {
        return mActivities.size();
    }


    private void activityCountIncrease(Activity activity) {
        if (activity != null) {
            String className = activity.getClass().getName();
            if (mActivityCount.containsKey(className)) {
                Integer i = mActivityCount.get(className);
                mActivityCount.put(className, i + 1);
            } else {
                mActivityCount.put(className, 1);
            }
        }
    }

    private void activityCountDecrease(Activity activity) {
        if (activity != null) {
            String className = activity.getClass().getName();
            if (mActivityCount.containsKey(className)) {
                Integer i = mActivityCount.get(className);
                if (i > 0) {
                    mActivityCount.put(className, i - 1);
                }
            }
        }
    }


    private void activityCountDecrease(Class activity) {
        if (activity != null) {
            String className = activity.getName();
            if (mActivityCount.containsKey(className)) {
                Integer i = mActivityCount.get(className);
                if (i > 0) {
                    mActivityCount.put(className, i - 1);
                }
            }
        }
    }

    public int count(Activity activity) {
        if (activity == null) return 0;
        String className = activity.getClass().getName();
        if (mActivityCount.containsKey(className)) {
            return mActivityCount.get(className);
        }
        return 0;
    }

    public int count(Class activity) {
        if (activity == null) return 0;
        String className = activity.getName();
        if (mActivityCount.containsKey(className)) {
            return mActivityCount.get(className);
        }
        return 0;
    }
}
